<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <!-- <el-form-item label="渠道编号" prop="channelNumber">
        <el-input
          v-model="queryParams.channelNumber"
          placeholder="请输入渠道编号"
          clearable
          size="small"
        />
      </el-form-item> -->
      <el-form-item label="渠道名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入渠道名称"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="后台账号" prop="managerAccount">
        <el-input
          v-model="queryParams.managerAccount"
          placeholder="请输入后台账号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="支付宝名字" prop="aliName">
        <el-input
          v-model="queryParams.aliName"
          placeholder="请输入支付宝名字"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="支付宝账号" prop="aliAccount">
        <el-input
          v-model="queryParams.aliAccount"
          placeholder="请输入支付宝账号"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="channelList">
      <el-table-column label="二级渠道名称" align="center" prop="name" />
      <el-table-column label="一级渠道名称" align="center" prop="name" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="微信号" align="center" prop="contactPerson" />
      <el-table-column label="支付宝名字" align="center" prop="aliName" />
      <el-table-column label="支付宝账号" align="center" prop="aliAccount" />
      <el-table-column label="申请时间" align="center" prop="openDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
         <template slot-scope="scope">
          <span v-if="scope.row.applyStatus===1">待审</span>
          <span v-else-if="scope.row.applyStatus===2">通过</span>
          <span v-else>不通过({{scope.row.applyText}})</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.applyStatus===1"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改渠道管理对话框 -->
    <el-dialog title="二级渠道审核" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="二级渠道名称：">
          {{form.name}}
        </el-form-item>
        <el-form-item label="渠道编号：" >
          {{form.name}}
        </el-form-item>
        <el-form-item label="后台帐号" >
          {{form.name}}
        </el-form-item>
        <el-form-item label="渠道审核：" prop="applyStatus">
          <el-radio-group v-model="form.applyStatus">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见: ">
          <el-input type="textarea" v-model="form.applyText" :rows="3"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listChannel, getChannel, delChannel, addChannel, updateChannel, exportChannel } from "@/api/system/channel";
import { loadUser } from "@/api/system/user";

import towChannel from "./towChannel";

export default {
  name: "Channel",
  components:{
    towChannel
  },
  data() {
    return {
      towChannelVisable: true,
      userOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 渠道管理表格数据
      channelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: 0,
        channelNumber: undefined,
        parentBusiness: undefined,
        name: undefined,
        contactPerson: undefined,
        contactPhone: undefined,
        wechat: undefined,
        managerAccount: undefined,
        managerPwd: undefined,
        aliName: undefined,
        aliAccount: undefined,
        proveNumber: undefined,
        settle: undefined,
        settleUnitPrice: undefined,
        chatroomCommissionRatio: undefined,
        otherCommissionRatio: undefined,
        maxRegister: undefined,
        createDate: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applyStatus: [
          { required: true, message: "渠道编号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    loadUser().then(response => {
      this.userOptions = response.data;
    });
  },
  methods: {
    /** 查询渠道管理列表 */
    getList() {
      this.loading = true;
      listChannel(this.queryParams).then(response => {
        this.channelList = response.rows;
        this.total = parseInt(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        channelNumber: undefined,
        parentBusiness: undefined,
        name: undefined,
        contactPerson: undefined,
        contactPhone: undefined,
        wechat: undefined,
        managerAccount: undefined,
        managerPwd: undefined,
        aliName: undefined,
        aliAccount: undefined,
        proveNumber: undefined,
        settle: undefined,
        settleUnitPrice: undefined,
        chatroomCommissionRatio: undefined,
        otherCommissionRatio: undefined,
        maxRegister: undefined,
        createDate: undefined,
        createBy: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleShowTwo(row){
      this.towChannelVisable = true
      this.$refs.towChannel.init(row)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加渠道管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getChannel(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改渠道管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateChannel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addChannel(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(id, status) {
      this.$confirm(`是否确认${status===0?'恢复':'禁用'}渠道管理编号为${id}的数据项?`, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delChannel(id, status);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有渠道管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportChannel(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>